package com.example.log.alarm.manager;

import ch.qos.logback.classic.spi.ILoggingEvent;
import com.example.log.alarm.repository.Repository;
import com.example.log.alarm.spy.QueuedSpy;
import com.example.log.alarm.util.BootstrapSpyUtil;

import java.util.Map;

/**
 * @program: spring-agent
 * @description:
 * @author: zt22328 赵儒定
 * @create: 2023-05-05 16:23
 **/
public class ErrorLogManager extends Thread {

    private Repository repository;

    private ThreadPoolManager threadPoolManager;

    public ErrorLogManager(Repository repository) {
        this.repository = repository;
    }

    public ErrorLogManager(Repository repository, ThreadPoolManager threadPoolManager) {
        this.repository = repository;
        this.threadPoolManager = threadPoolManager;
    }

    @Override
    public void run() {
        while (true) {

            try { Thread.sleep(5000); } catch (InterruptedException e) { }

            for (Map<String, Object> map : BootstrapSpyUtil.getThrowable(1000)) {
                try { repository.putThrowable(map); } catch (Exception e) { }
                if (threadPoolManager != null) {
                    try { threadPoolManager.incrementError(map); } catch (Exception e) { }
                }
            }

            for (ILoggingEvent event : QueuedSpy.getLoggingEvents(1000)) {
                try { repository.putLogger(event); } catch (Exception e) { }
            }
        }
    }

}
